﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static WHUIdemo.Forms.POSUIDemo;
using WHControlLib.CommonHelpers;
using WHControlLib.Controls;
using WHControlLib.Forms;
using WHControlLib.Controls.Goods;
using WHUIdemo.PageFrms;

namespace WHUIdemo.Forms.PosDemoPageFrm
{





    public partial class SaleFrm : Form
    {
        ListCtrlPageManager ItemListManager = new ListCtrlPageManager();
        ListCtrlPageManager ItemClassListManager = new ListCtrlPageManager();

        List<Goods> GoodsItemList = new List<Goods>();

        List<WHButton> itemWHButtons = new List<WHButton>();
        List<WHButton> itemClassWHButtons = new List<WHButton>();
        int HCount = 5;

        int VCount = 4;
        int ClassHCount = 5;
        int ClassVCount = 2;
        List<Item> ItemList = new List<Item>();
        List<ItemClass> ItemClassList = new List<ItemClass>();

        List<Item> DisplayItemList;
        List<ItemClass> DisplayItemClassList;

        int ItemnowPage = 1;
        int TotleItemPage;

        int ItemClassNowPage = 1;
        int TotleItemClassPage;
        int ItemClassOnePageCount;

        int GoodsNowPage = 1;
        int GoodsOnePageCount = 5;
        int TotleGoodsPage;
        int CurrentSelectedGoods= 0;
        public SaleFrm()
        {
            InitializeComponent();
        }
        public static SaleFrm  OnlyoneFrm = null;
        public static SaleFrm CreatForm()
        {
            if (OnlyoneFrm==null||OnlyoneFrm.IsDisposed)
            {
                OnlyoneFrm= new SaleFrm();

            }
            return OnlyoneFrm; }

        private void whButton7_Click(object sender, EventArgs e)
        {
            if (ItemnowPage < TotleItemPage)
            {
                ItemnowPage++;
            }
            IteomToCtrls(ItemList, 20, ItemnowPage);


            ItemListManager.SetSplitedCtrlListToParent(itemWHButtons,
                                                       itemPanel, HCount, VCount, 5);
        }

        private void SaleFrm_Load(object sender, EventArgs e)
        {

            //商品初始化
            ItemAutoAdd();
            IteomToCtrls(ItemList, 20, 1);
            ItemListManager.SetSplitedCtrlListToParent(itemWHButtons,
                                                              itemPanel, HCount, VCount, 5);
            //商品类别初始化
            ItemClassOnePageCount = ClassHCount * ClassVCount;
            ItemClassAutoAdd();

            ItemClassToCtrls(ItemClassList, ItemClassOnePageCount, ItemClassNowPage);
            ItemListManager.SetSplitedCtrlListToParent(itemClassWHButtons,
                                                                      panelCLass, ClassHCount, ClassVCount, 2);
        }
        private void ItemAutoAdd()
        {
            int itmeClassCount = 35;
            for (int i = 1; i <= itmeClassCount; i++)
            {
                Item item = new Item();
                item.Name = "商品" + i.ToString();
                item.Price = (float)(i + 10.00);
                item.Unit = "个";
                item.barcode = "69" + (i + 1000).ToString();
                ItemList.Add(item);


            }


        }
        private void ItemClassAutoAdd()
        {
            int itmeClassCount = 12;
            for (int i = 1; i <= itmeClassCount; i++)
            {
                ItemClass itemclass = new ItemClass();
                itemclass.ItemClassName = "类别" + i.ToString();

                ItemClassList.Add(itemclass);


            }


        }

        void IteomToCtrls(List<Item> ItemList, int OnepageCount, int NowPage)
        {
            itemWHButtons.Clear();

            DisplayItemList = ItemListManager.GetSplitedPageList(ItemList, 20, NowPage, out TotleItemPage);


            for (int d = 0; d < DisplayItemList.Count; d++)
            {
                WHButton wb = new WHButton();
                wb.Text = DisplayItemList[d].Name + "\n价格：" + DisplayItemList[d].Price.ToString();
                wb.FirstFillcolor = Color.Moccasin;
                wb.SecondFillcolor = Color.DarkOrange;
                wb.BornColor = Color.Black;
                wb.IsDrawBoin = true;
                wb.IsUseTwoColor = true;
                wb.FontColor = Color.Black;
                wb.OnMouseColor = Color.SkyBlue;
                wb.ColorChangeint = 50;
                wb.IsCanGetFouce = false;
                wb.IsShowFouceLine = false;
                string msg = DisplayItemList[d].barcode + "," + DisplayItemList[d].Name + "," + DisplayItemList[d].Price.ToString()
                               +","+DisplayItemList[d].Unit;
                wb.Tag = msg;
                wb.Click += ItemButtonClick;
                itemWHButtons.Add(wb);
            }

        }



        void ItemButtonClick(Object Sender,EventArgs e)
        {
            WHButton B = (WHButton)Sender;
            string s=B.Tag.ToString().Trim();
            string[] ArrayStr = s.Split(',');
               Goods good = new Goods();
            if (ArrayStr.Length>1)
            {
                
                    good.barcode = ArrayStr[0];
                    good.Name = ArrayStr[1];
                good.Price = Convert.ToDouble(ArrayStr[2]);
                good.Unit=ArrayStr[3];  
                
                GoodsItemList.Add(good);
                 good.Serial=GoodsItemList.IndexOf(good)+1;                
            }

            GoodsPanel.AddToDisplayCtrlList(good);

            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);
            //  int thisGoodsIndex = GoodsItemList.IndexOf(good);
            //int thisGoodsPage=  thisGoodsIndex / GoodsOnePageCount+1;
            //  CurrentSelectedGoods = thisGoodsIndex;


        }
      



        void ItemClassToCtrls(List<ItemClass> ItemClassList, int OnepageCount, int NowPage)
        {
            itemClassWHButtons.Clear();

            DisplayItemClassList = ItemListManager.GetSplitedPageList(ItemClassList, ItemClassOnePageCount, ItemClassNowPage, out TotleItemClassPage);


            for (int d = 0; d < DisplayItemClassList.Count; d++)
            {
                WHButton wb = new WHButton();
                wb.Text = DisplayItemClassList[d].ItemClassName;
                wb.FirstFillcolor = Color.Violet;
                wb.FontColor = Color.White;
                wb.OnMouseColor = Color.DeepPink;
                wb.IsShowFouceLine = false;
                wb.FirstFillcolor = Color.SkyBlue;
                wb.SecondFillcolor = Color.DarkBlue;
                wb.BornColor = Color.White;
                wb.IsDrawBoin = true;
                wb.IsUseTwoColor = true;


                itemClassWHButtons.Add(wb);
            }

        }

        private void SaleFrm_SizeChanged(object sender, EventArgs e)
        {


            ItemListManager.SetSplitedCtrlListToParent(itemWHButtons,
                                                       itemPanel, HCount, VCount, 5);

            ItemListManager.SetSplitedCtrlListToParent(itemClassWHButtons,
                                                                    panelCLass, ClassHCount, ClassVCount, 2);
        
        }

        private void whButton6_Click(object sender, EventArgs e)
        {
            if (ItemnowPage > 1)
            {
                ItemnowPage--;
            }
            IteomToCtrls(ItemList, 20, ItemnowPage);
            ItemListManager.SetSplitedCtrlListToParent(itemWHButtons,
                                                       itemPanel, HCount, VCount, 5);
        }

        private void whButton9_Click(object sender, EventArgs e)
        {
            if (ItemClassNowPage < TotleItemClassPage)
            {
                ItemClassNowPage++;
            }

            ItemClassToCtrls(ItemClassList, ItemClassOnePageCount, ItemClassNowPage);

            ItemListManager.SetSplitedCtrlListToParent(itemClassWHButtons, panelCLass, ClassHCount, ClassVCount, 2);

        }

        private void whButton8_Click(object sender, EventArgs e)
        {
            if (ItemClassNowPage > 1)
            {
                ItemClassNowPage--;
            }

            ItemClassToCtrls(ItemClassList, ItemClassOnePageCount, ItemClassNowPage);

            ItemListManager.SetSplitedCtrlListToParent(itemClassWHButtons, panelCLass, ClassHCount, ClassVCount, 2);
        }

        private void SaleFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnlyoneFrm = null;
        }

        private void whButton1_Click(object sender, EventArgs e)
        {
            GoodsPanel.SetGoodsAmoutAddOne();
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);
        }

        private void whButton4_Click(object sender, EventArgs e)
        {
            GoodsPanel.NextPage();
        }

        private void whButton3_Click(object sender, EventArgs e)
        {
            GoodsPanel.UpPage();
        }

        private void DelGoodsBt_Click(object sender, EventArgs e)
        {
            GoodsPanel.RemoveFromDisplayCtrl();

            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);


        }

        private void GoodsGiftBt_Click(object sender, EventArgs e)
        {
            GoodsPanel.SetGoodsGiftInfo();
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);
        }

        private void whButton2_Click(object sender, EventArgs e)
        {
            GoodsPanel.SetGoodsAmoutDecreaseOne();
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);
        }

        private void GoodsAmountBt_Click(object sender, EventArgs e)
        {
            if (GoodsPanel.CurrentSelectedGoods>-1)
            {
                Goods ingoods = GoodsPanel.DisplayGoodsItemList[GoodsPanel.CurrentSelectedGoods];   
               GoodsChangeAmountFrm goodsChangAmoutFrm= new GoodsChangeAmountFrm(ingoods);
                goodsChangAmoutFrm.TranAmountToGoods += ChangeCurrentGoodsAmout;
                goodsChangAmoutFrm.ShowDialog();
            }
          

        }

        public void ChangeCurrentGoodsAmout(double d)
        {
            GoodsPanel.ChangeGoodsAmount(d);
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber) ;
        }
        public void ChangCurrentGoodsPrice(double d)
        {
            GoodsPanel.ChangeGoodsPrice(d);
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);
        }

        public void SetCurrentGoodsDiscount(double d)
        {
            
            GoodsPanel.SetGoodsDicountInfo(d);
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);

        }
        private void GoodsDiscountBt_Click(object sender, EventArgs e)
        {

            if (GoodsPanel.CurrentSelectedGoods > -1)
            {
                Goods ingoods = GoodsPanel.DisplayGoodsItemList[GoodsPanel.CurrentSelectedGoods];
          
               
            ChangeGoodsDiscountFrm ChGoodsFrm = new ChangeGoodsDiscountFrm(ingoods);
                ChGoodsFrm.TranDiscontTogoods += SetCurrentGoodsDiscount;
                ChGoodsFrm.ShowDialog();
            }


        }


        void DisPlaySumPrice(double sumPrice,double sumAmout)
        {
            this.Invoke(new Action(() =>
            {
                if (sumPrice==0)
                {
                    CheckOutButton.Text = "结账     ";
                }
                else
                CheckOutButton.Text = "结账     ￥ " + sumPrice.ToString("F2");
                if (sumAmout==0)
                {
                    SumAmountLB.Text = "共计: ";

                }else

                    SumAmountLB.Text = "共计: " + sumAmout.ToString() + " 件";
                //SumAmountLB.Invalidate();
                //CheckOutButton.Invalidate();
            }));
          

        }

        private void ChangePriceBT_Click(object sender, EventArgs e)
        {
           
            if (GoodsPanel.CurrentSelectedGoods > -1) {
                Goods ingoods = GoodsPanel.DisplayGoodsItemList[GoodsPanel.CurrentSelectedGoods];
                ChangCoodsPriceFrm ChGoodsFrm = new ChangCoodsPriceFrm(ingoods);
                ChGoodsFrm.TranPriceGoods += ChangCurrentGoodsPrice;
                ChGoodsFrm.ShowDialog();
            }
               
        }

      public  void Del_SetLastOrderInfo(double LastRealPayMoney, double LastRecevialblePayMoney, double LastCheckOutMoney)
        {
            GoodsPanel.GetLastOrderInfo(LastRealPayMoney, LastRecevialblePayMoney, LastCheckOutMoney);

        }
        private void ClearBtn_Click(object sender, EventArgs e)
        {
            GoodsPanel.ClearAll();
            CheckOutButton.Text = "结账 (未选商品） ";
         
           
            SumAmountLB.Text = "共计（未选商品）";
            //SumAmountLB.Invalidate(); 
            //CheckOutButton.Invalidate();

        }

        private void SaleFrm_Activated(object sender, EventArgs e)
        {
            SearchBox.Focus();
        }
      /// <summary>
      /// 完成结账操作后清空商品并初始化
      /// </summary>
        void ClearAllDisplayGoods()
        {

              GoodsPanel.ClearAll();
            DisPlaySumPrice(GoodsPanel.AllSumGoodsPrice, GoodsPanel.AllGoodsSumItemNumber);

        }

        private void CheckOutButton_Click(object sender, EventArgs e)
        {
            GoodsCheckOutInfo CheckOutInfo = new GoodsCheckOutInfo();
            CheckOutInfo.ReceivableMoney = GoodsPanel.AllSumGoodsReceivablePrice;
            CheckOutInfo.RealPayMoney = GoodsPanel.AllSumGoodsPrice;
            CheckOutInfo.GoodsAmoutn = GoodsPanel.AllGoodsSumItemNumber;
            CheckOutInfo.ReceivableMoney = GoodsPanel.AllSumGoodsReceivablePrice;

            GoodsCheckOutFrm CheckOutfrm = new GoodsCheckOutFrm(CheckOutInfo);
            CheckOutfrm.checkOutOverClear += ClearAllDisplayGoods;
            CheckOutfrm.Del_getlastOrderInfo += Del_SetLastOrderInfo;
            CheckOutfrm.ShowDialog();

        }
    }


    public class Item
    {
        public string Name;
        public float Price;
        public string barcode;
        public string Unit;
        public  int  ClassId;
    }
    public class ItemClass
    {
        public int ClassId;
        public string ItemClassName;
    }

    public class GoodsCheckOutInfo
    {
        public double ReceivableMoney;
        public double RealPayMoney;
        public int GoodsAmoutn;
        public string DH;
        public string MemberId;
        public string MemberName;

    }

  
}
